import java.util.Arrays;

/**
 * Created with Intellij IDEA
 * Description
 * User:ASUS
 * Date:2024-08-10
 * Time:20:19
 */
public class MyArrayList implements IList{
    public int[] array;
    public int usedSize;
    public MyArrayList(){
        this.array=new int[10];
    }

    @Override
    //默认放在 最后
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.array[this.usedSize]=data;
        this.usedSize++;
    }

    private void grow(){
        this.array= Arrays.copyOf(this.array,2*this.array.length);
    }
    @Override
    public void add(int pos, int data) {
        try {
            checkPos(pos);
            if (isFull()){
                grow();
            }
            //挪动元素
            for (int i=usedSize-1;i>=pos;i--){
                array[i+1]=array[i];
            }
            array[pos]=data;
            usedSize++;
        }catch (PosIllegal e){
            System.out.println("插入元素pos位置不合法");
            e.printStackTrace();
        }
    }

    public boolean isFull(){
        return this.usedSize==array.length;
    }
    private void checkPos(int pos){
        if(pos<0||pos>usedSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }
    @Override
    public boolean contains(int toFind) {
        for (int i=0;i<usedSize;i++){
            if (array[i]==toFind){
                return true;
            }
        }
        return false;
    }

    //包含toFind返回下标
    //否则返回-1
    @Override
    public int indexOf(int toFind) {
        for (int i=0;i<usedSize;i++){
            if (array[i]==toFind){
                return i;
            }
        }
        return 0;
    }
    private void checkPos2(int pos){
        if(pos<0||pos>=usedSize){
            throw new PosIllegal("Pos位置不合法");
        }
    }
    @Override
    public int get(int pos) {
        try {
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyExcetion e){
            e.printStackTrace();
        }
        return -1;
    }
    private void checkEmpty(){
        if(isEmpty()){
            throw new EmptyExcetion("顺序表为空");
        }
    }
    public boolean isEmpty(){
        return usedSize==0;
    }
    @Override
    public void set(int pos, int value) {
        try {
            checkEmpty();
            checkPos2(pos);
            array[pos]=value;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyExcetion e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int toRemove) {
        try {
            checkEmpty();
            int pos=indexOf(toRemove);
            if (pos==-1){
                return;
            }
            for (int i=pos;i<usedSize-1;i++){
                array[i]=array[i+1];
            }
            usedSize--;
        }catch (EmptyExcetion e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        usedSize=0;
    }
    public void display(){
        for (int i=0;i<this.usedSize;i++){
            System.out.println(array[i]+" ");
        }
    }
}
